﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServiceConfigTool
{
    public class SIP2Msg
    {
        
        public SIP2Msg()
        {                      
        }
        
        
        #region 校验码
        /// <summary>
        /// 校验码
        /// </summary>
        /// <param name="szCMD">AZ及之前的命令</param>
        /// <returns>生成的校验码</returns>
        private static int GetChecksum(ref string szCMD)
        {
            try
            {
                ushort num1 = 0;
                int num2 = szCMD.Length;
                string text2 = szCMD;
                for (int num5 = 0; num5 < text2.Length; num5++)
                {
                    ushort num3 = text2[num5];
                    num1 = (ushort)(num1 + num3);
                }
                num1 = (ushort)(~num1 + 1);
                string text1 = num1.ToString("X").PadLeft(4, '0');
                szCMD = szCMD + text1;
                // szCMD = szCMD + Environment.NewLine;
            }
            catch (Exception)
            {
                return 0;
            }
            return 1;
        }
        #endregion


        #region SC->ACS 登录
        public static string LoginMsg(string szUser, string szUserPWD)
        {
            string text1 = "";
            text1 = text1 + "93";
            text1 = text1 + "00";
            text1 = text1 + "CN";
            text1 = text1 + szUser;
            text1 = text1 + "|";
            text1 = text1 + "CO";
            text1 = text1 + szUserPWD;
            text1 = text1 + "|";
            text1 = text1 + "CP|AY1AZ";
            if (1 != GetChecksum(ref text1))
            {
                return null;
            }
            return text1;

        }
        #endregion


        #region SC->ACS借阅者信息
        /// <summary>
        /// SC->ACS借阅者信息
        /// </summary>
        /// <param name="readerCardId">借阅者卡号</param>
        /// <param name="readerPWD">借阅者密码</param>
        /// <returns></returns>
        public static  string ReaderInfoMsg(string readerCardId, string readerPWD)
        {

            StringBuilder sb = new StringBuilder();
            sb.Append("63"); // 63号命令
            sb.Append("000"); // 语言
            sb.Append(DateTime.Now.ToString("yyyyMMdd    HHmmss")); //执行时间，注意有4个空格
            sb.Append("Y     "); // 占位符，第0位表示"hold items"
            sb.Append("AO107"); // SC机构标识044120
            sb.Append("|");
            sb.Append("AA" + readerCardId); // 借阅者卡号
            sb.Append("|");
            sb.Append("AD" + readerPWD); // 借阅者密码
            sb.Append("|");
            sb.Append("AY1AZ"); // 准备校验码
            string text1 = sb.ToString().Trim();
            if (1 != GetChecksum(ref text1))
            {
                return null;
            }
            return text1;
        }
        #endregion


        #region SC->ACS图书信息
        /// <summary>
        /// SC->ACS图书信息
        /// </summary>
        /// <param name="bookid">图书条码或RFID</param>
        /// <returns></returns>
        public static string BookInfoMsg(string bookid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("17"); // 17号命令
            sb.Append(DateTime.Now.ToString("yyyyMMdd    HHmmss")); //执行时间，注意有4个空格
            sb.Append("AO107"); // SC机构标识044120
            sb.Append("|");
            sb.Append("AB" + bookid.ToUpper().Trim()); // 图书id（条码）
            sb.Append("|");
            sb.Append("AC"); // 终端密码，此处不提供
            sb.Append("|");
            sb.Append("AY1AZ"); // 准备校验码
            string text1 = sb.ToString().Trim();
            if (1 != GetChecksum(ref text1))
            {
                return null;
            }
            return text1;
        }
        #endregion


        #region SC->ACS还书命令
        /// <summary>
        /// SC->ACS还书命令
        /// </summary>
        /// <param name="bookid">条码或RFID</param>
        /// <returns>ACS->SC返回的命令</returns>
        public static string ReturnMsg(string bookid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("09"); // 09号命令
            sb.Append("N"); // 执行
            DateTime time1 = DateTime.Now;
            sb.Append(time1.ToString("yyyyMMdd    HHmmss")); //执行时间，注意有4个空格
            sb.Append(time1.ToString("yyyyMMdd    HHmmss")); //执行时间，注意有4个空格
            sb.Append("AP"); // 还书地点，未指定
            sb.Append("|");
            sb.Append("AO107"); // SC机构标识044120
            sb.Append("|");
            sb.Append("AB" + bookid.Trim()); // 图书id（条码）
            sb.Append("|");
            sb.Append("AC"); // 终端密码，此处不提供
            sb.Append("|");
            sb.Append("AY1AZ"); // 准备校验码
            string text1 = sb.ToString().Trim();
            if (1 != GetChecksum(ref text1))
            {
                return null;
            }
            return text1;
        }
        #endregion


        #region SC->ACS借书命令
        /// <summary>
        /// SC->ACS借书命令
        /// </summary>
        /// <param name="readerCardId">借阅者证件号</param>
        /// <param name="bookid">图书id（条码）</param>
        /// <param name="termPWD">终端密码，此处不提供</param>
        /// <param name="readerCardPWD">证件密码（非必须）</param>
        /// <returns></returns>
        public static string BorrowMsg(string readerCardId, string bookid, string termPWD,string readerCardPWD)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("11"); // 11号命令
            sb.Append("Y"); // Y：该书可以续借；N：该书不能续借
            sb.Append("N"); 
            DateTime time1 = DateTime.Now;
            sb.Append(time1.ToString("yyyyMMdd    HHmmss")); //执行时间，注意有4个空格
            sb.Append(time1.ToString("yyyyMMdd    HHmmss")); //执行时间，注意有4个空格
            sb.Append("AO107"); // SC机构标识044120
            sb.Append("|");
            sb.Append("AA" + readerCardId.Trim()); // 借阅者证件号
            sb.Append("|");
            sb.Append("AB" + bookid.Trim()); // 图书id（条码）
            sb.Append("|");
            sb.Append("AC" + termPWD.Trim()); // 终端密码，此处不提供
            sb.Append("|");
            sb.Append("AD" + readerCardPWD.Trim()); // 证件密码（非必须）
            sb.Append("|");
            sb.Append("AY1AZ"); // 准备校验码
            string text1 = sb.ToString().Trim();
            if (1 != GetChecksum(ref text1))
            {
                return null;
            }
            return text1;
        }

        #endregion


        #region SC->ACS续借命令
        public static string RenewMsg(string readerCardId, string readerCardPWD, string bookId)
        {
            string text1 = "";
            text1 = text1 + "29";
            text1 = text1 + "Y";
            text1 = text1 + "N";
            text1 = text1 + DateTime.Now.ToString("yyyyMMdd    HHmmss");
            text1 = text1 + DateTime.Now.ToString("yyyyMMdd    HHmmss");
            text1 = text1 + "AO107|";
            text1 = text1 + "AA";
            if (null != readerCardId)
            {
                text1 = text1 + readerCardId;
            }
            text1 = text1 + "|";
            text1 = text1 + "AD";
            if (null != readerCardPWD)
            {
                text1 = text1 + readerCardPWD;
            }
            text1 = text1 + "|";
            text1 = text1 + "AB";
            if (null != bookId)
            {
                text1 = text1 + bookId;
            }
            text1 = text1 + "|";
            text1 = text1 + "AC|";
            text1 = text1 + "CH|";
            text1 = text1 + "BOY";
            text1 = text1 + "AY3AZ";
            if (1 != GetChecksum(ref text1))
            {
                return null;
            }
            return text1;
        }
        #endregion

    }
}
